using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using MacomberMap.Common.Internals;
using MacomberMap.Common.User_Interfaces.Components;
using MacomberMap.Common.Components;

namespace MacomberMap.Common.Convertors
{
    /// <summary>
    /// This class allows for conversions between strings and MM_KVLevels.
    /// </summary>
    public class MM_Boundary_Locator : TypeConverter
    {
        /// <summary>
        /// Indicate standard values are supported
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        /// <summary>
        /// Inidicate standard values are supported exclusively
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }

        /// <summary>
        /// Return a standard value collection of boundaries by string
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new System.ComponentModel.TypeConverter.StandardValuesCollection(MM_Repository.Counties.Keys);
        }

        /// <summary>
        /// Determine if you can convert from a type to a boundary
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sourceType"></param>
        /// <returns></returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true; 
            else
                return base.CanConvertFrom(context, sourceType);
        }

        /// <summary>
        /// Determine if you can convert from a boundary to a type
        /// </summary>
        /// <param name="context"></param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(MM_Boundary))
                return true;
            else
                return base.CanConvertTo(context, destinationType);
        }

        /// <summary>
        /// Convert a string to a boundary
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (value is string && (string)value == "STATE")
                return MM_Repository.StateBoundary;
            else if (value is string)
                return MM_Repository.Counties[value as string];
            else
                return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}
